/**
 * 系统信息服务
 */

import type { PlasmoMessaging } from "@plasmohq/messaging"

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
  const cpuInfo = await chrome.system.cpu.getInfo()
  const memInfo = await chrome.system.memory.getInfo()
  const chromeVersion = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2]
  const systemInfo = {
    cpu: cpuInfo.modelName,
    cores: cpuInfo.numOfProcessors,
    memMax: (memInfo.capacity / (1024 * 1024 * 1024)).toFixed(2), // 转换为GB并保留两位小数
    memCur: (
      (memInfo.capacity - memInfo.availableCapacity) /
      (1024 * 1024 * 1024)
    ).toFixed(2), // 转换为GB并保留两位小数
    version: process.env.PLASMO_PUBLIC_DG_VERSION,
    // chrome版本号，由于隐私问题，不提供具体版本号
    chromeVersion: "v" + chromeVersion,
    infoTxt: ""
  }
  // 增加组合信息字段
  systemInfo.infoTxt = `${systemInfo.version}_[${systemInfo.cpu}(${systemInfo.cores}核) ${systemInfo.memMax}GB/${systemInfo.memCur}GB]_${systemInfo.chromeVersion}`
  res.send(systemInfo)
}

export default handler
